<?php
!defined('P_W') && exit('Forbidden');
require_once (R_P . 'lib/base/systemdata.php');
require_once (R_P . 'require/showimg.php');
/**
 * 经纪人数据调用
 * @author bingbing.renbb
 *
 */
class PW_BrokerSource extends SystemData {
	var $_lang = array(
		'uid' => '经纪人ID',
		'realname' => '姓名',
		'tel' => '联系电话',
		'plate' => '服务商圈',
		'image' => '头像',
		'salenum' => '出售数量',
		'hirenum' => '出租数量',
		'online' => '在线状态图标'
	);
	
	/**
	 * 获取提交的数据
	 * @param array $config
	 * @param int $num
	 * @return array $result
	 */
	function getSourceData($config, $num) {
		$config = $this->_initConfig($config);
		$_tmp = $this->_getData($config, $num);
		$_tmp = $_tmp ? $_tmp : array();
		$_userids = array();
		foreach ($_tmp as $key => $value) {
			$_tmp[$key] = $this->_cookData($value);
			$_userids[$key] = $value['uid'];
		}
		$_info = $this->_getFacesAndOnline($_userids);
		foreach ($_tmp as $key => $value) {
			$value && $_info[$value['uid']] && $result[] = array_merge($value, $_info[$value['uid']]);
		}
		return $result;
	}
	
	/**
	 * 选择项
	 * @return array
	 */
	function getSourceConfig() {
		return array(
			'sort' => array(
				'name' => '排序方式',
				'type' => 'select',
				'value' => array(
					'salenum' => '出售房数量',
					'hirenum' => '出租房数量',
					'lastsaletime' => '最近发布',
					'createtime' => '开通时间'
				)
			)
		);
	}
	
	/**
	 * 处理传入的数据
	 * @param array $config
	 * @return array $temp
	 */
	function _initConfig($config) {
		$temp = array();
		$temp['sort'] = $config['sort'];
		return $temp;
	}
	
	/**
	 * 查找符合条件的数据
	 * @param array $config
	 * @param int $num
	 * @return array
	 */
	function _getData($config, $num) {
		$brokerService = $this->_getBrokerService();
		$result = $brokerService->findBrokers(array(), 1, $num, $config['sort']);
		return $result[0];
	}
	
	/**
	 * 处理数据 
	 * @param unknown_type $data 数据库取得的数据
	 * @return unknown_type $data 处理后的数据
	 */
	function _cookData($data) {
		if (!S::isArray($data) || empty($data['uid'])) return $data;
		$data['plate'] = $this->_getPlateInfo($data['plate']);
		$data['url'] = 'index.php?m=house&q=shop&uid=' . $data['uid'];
		$data['title'] = $data['realname'];
		$data['tel'] = $data['phone'] ? $data['phone'] : $data['tel'];
		return $data;
	}
	/**
	 * 获取服务商圈数据
	 * @param array $plate 服务商圈信息数组
	 * @return string $temp 服务商圈信息
	 */
	function _getPlateInfo($plate) {
		if (!S::isArray($plate)) return '';
		$housefieldsService = House::loadService('HouseFieldsService');/* @var $housefieldsService PW_HouseFieldsService*/;
		$allPlate = $housefieldsService->getCompsiteFieldsByType('plate');
		$temp = '';
		foreach ($plate as $value) {
			$temp .= ' '.$allPlate[$value];
		}
		return $temp;
	}
	
	/**
	 * 获取头像跟在线状态
	 * @param mixed $ids 用户id
	 * @return array  $results 头像跟在线状态
	 */
	function _getFacesAndOnline($ids) {
		is_numeric($ids) && $ids = array($ids);
		if (!S::isArray($ids)) return array();
		$userService = $this->_getUserService();
		$infos = $userService->getUsersWithMemberDataByUserIds($ids);
		$results = array();
		foreach ($infos as $key => $value) {
			$faceurl = showfacedesign($value['icon'], 1, 'm');
			$online = $this->_getOnline($value['thisvisit']);
			$results[$key] = array('image'=>$faceurl[0], 'online'=>$online);
		}
		return $results;
	}
	
	/**
	 * 在线状态图标
	 * @param integer $time 时间戳
	 * @return string 图片标签
	 */
	function _getOnline($time) {
		if (checkOnline($time)) {
			return '<img src="images/online.png" title="在线" alt="在线" class="icon-img" />';	
		} 
		return '<img src="images/stealth.png" title="离线" alt="离线" class="icon-img" />';
	}
	
	/**
	 * 取得用户服务
	 */
	function _getUserService() {
		return L::loadClass('UserService', 'user');
	}
	
	/**
	 * 载入经纪人服务类
	 */
	function _getBrokerService() {
		return House::loadService('BrokerService');
	}
}
?>